-- enter_do_summon
-- create by zhuangsl
-- 进入迷宫时进行召唤，由服务器处理

return {
    apply = function(prop, baseValue)
        -- 提升的值
        return prop[3];
    end,

    -- 进入迷宫触发
    trigger = function(who, prop, enter)
        local classId = tonumber(prop[3]);
        if who.type ~= OBJECT_TYPE_USER then
            return;
        end

        local summonInfo = who.dbase:query("summon", {});

        if not enter then
            -- 就是进入下一层，如果当前没有该召唤物，且身上没有掉落物，自动奖励之
            local found = false;
            for _, data in pairs(summonInfo) do
                if SummonM.query(data["class_id"], "rule") == SummonM.query(classId, "rule") then
                    found = true;
                    break;
                end
            end

            -- 改召唤物还未死亡
            if found then
                return;
            end

            -- 判断是否有掉落
            local dbase = SummonM.query(classId, "dbase") or {};
            local bonus = dbase["dead_bonus"];
            if not bonus or type(bonus[1]) ~= "number" then
                return;
            end

            -- 不能自动拾取
            if dbase["auto_pick"] ~= 1 then
                return;
            end

            -- 已经拾取过了
            if ItemM.getAmount(who, bonus[2]) >= 1 then
                return;
            end

            -- 阿兹猫已经提交给树屋了
            local treeHouse = ME.user.tree_house;
            if SummonM.query(classId, "rule") == "azrael_cat"
                and treeHouse and treeHouse:isActive() then
                local allSkills = treeHouse:getAllSkills();
                for skillId, _ in pairs(allSkills) do
                    if TreeHouseM.querySkill(skillId, "rule") == "die_cat" then
                        return;
                    end
                end
            end

            BonusM.doBonus(bonus, "auto_pick");
        end
    end,

    record = function(target, prop, path)
        return false;
    end
};
